<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 10:55
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;
use think\facade\View;
use think\facade\Filesystem;

class policy extends AdminBase
{
    public function index()
    {
        $agency = \app\common\model\Agency::getAgency();
        return View::fetch('index',['agency'=>$agency]);
    }

    //分类管理
    public function policy_json()
    {
        $map = [['a.type','=',input('type')]];
        $aid = input('aid');
        $policy = input('policy');
        $bill = input('bill');
        $cid = input('cid');

        if(!empty($aid)){
            array_push($map,['a.aid','=',$aid]);
        }
        if(!empty($cid)){
            array_push($map,['a.cid','=',$cid]);
        }

        if(!empty($policy)){
            array_push($map,['a.name','like','%'.$policy.'%']);
        }

        if(!empty($bill)){
            array_push($map,['b.name','like','%'.$bill.'%']);
        }

        $list = Db::name('policy')->alias('a')->join('bill b','a.bid = b.id')->where($map)->order('a.create_time')->field('a.*,b.name as bill_name')->paginate(10)->each(function($item, $key){
            switch($item['car']){
                case '0':
                    $item['car'] = '新车';break;
                case '1':
                    $item['car'] = '旧车';break;
                default:
                    $item['car'] = '全部';break;
            }
            switch($item['displacement']){
                case '0':
                    $item['displacement'] = '<250';break;
                case '1':
                    $item['displacement'] = '>=250';break;
                default:
                    $item['displacement'] = '全部';break;
            }

            return $item;
        });

        $this->result($list);
    }

    //分类添加
    public function policy_add()
    {
        if ($this->request->isPost()) {
            $param = input('post.');

            $validate = new \app\admin\validate\Policy();
            $result = $validate->check($param);

            if(!$result){
                $this->error($validate->getError());
            }

            if($param['isedit'] == 0){
                $param['create_time'] = time();
            }
            $param['update_time'] =time();
            $param['uid'] = session('admin_auth.admin_id');
            if($param['isedit'] == 1){
                $book = \app\common\model\Policy::find($param['id']);
            }else{
                $book = new \app\common\model\Policy();
            }

            $msg = $book->allowfield([])->save($param);
            if($msg){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

        $aid = input('aid');
        $aname = Db::name('agency')->where('id',$aid)->value('name');
        $cid = input('cid');
        $cname = Db::name('company')->where('id',$cid)->value('name');
        $agency = \app\common\model\Agency::getAgency();
        $company = \app\common\model\Company::getCompany($aid);
        $bill = \app\common\model\Bill::getBill($aid,$cid);
        $isedit = input('isedit');
        $map =['isedit'=> $isedit,'agency'=>$agency,'company'=>$company,'bill'=>$bill,'aid'=>$aid,'aname'=>$aname,'cid'=>$cid,'cname'=>$cname,'type'=>input('type',0)];

        if($isedit == 1){
            $data = Db::name('policy')->where('id',input('id'))->find();
            $map['data'] = $data;
        }

        return $this->fetch('policy_edit',$map);
    }

    //分类删除
    public function policy_del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            if(session('admin_auth.admin_id') != 1) $this->error('无权进行此操作');

            Db::name('policy')->where('id','in',$param['id'])->delete();
            insert_admin_log('删除了保险公司');
            $this->success('删除成功');
        }
    }

    public function getCompany(){
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $data = Db::name('company')->where('aid',$param['id'])->field('name,id')->select();

            return $data;
        }
    }

    public function getBill(){
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $data = Db::name('bill')->where('cid',$param['id'])->field('name,id')->select();

            return $data;
        }
    }
}